﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace Riba.MVVMSL
{
	///<summary>
	///</summary>
	internal class MessageWindow: ChildWindow
	{
		private Action<object>_callback;
		private object _closingTag;

		///<summary>
		///</summary>
		///<param name="message"></param>
		///<param name="caption"></param>
		///<param name="callback"></param>
		///<param name="style"></param>
		public MessageWindow(string message, string caption, Action<object> callback, Style style)
		{
			var buttons = new List<MessageWindowButton>();
			buttons.Add(new MessageWindowButton { Caption = "OK" });

			Init(message, caption, buttons, callback, style);
		}

		///<summary>
		///</summary>
		///<param name="message"></param>
		///<param name="caption"></param>
		///<param name="buttons"></param>
		///<param name="callback"></param>
		///<param name="style"></param>
		public MessageWindow(string message, string caption, IList<MessageWindowButton> buttons, Action<object> callback, Style style)
		{
			Init(message, caption, buttons, callback, style);
		}

		private void Init(string message, string caption, IList<MessageWindowButton> buttons, Action<object> callback, Style style)
		{
			if ( buttons.Count > 4)
				throw new ArgumentOutOfRangeException("buttons", "Maximum 4 Buttons allowed");

			_callback = callback;
			double minWidth = 180;

			var grid = new Grid();
			grid.RowDefinitions.Add(new RowDefinition());
			grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(0, GridUnitType.Auto) });

			var stackPanel1 = new StackPanel();

			var textBlock = new TextBlock();
			textBlock.Text = "a";
			textBlock.Text = caption;
			minWidth = Math.Max(minWidth, textBlock.ActualWidth + 40);
			Title = caption;

			textBlock.Text = message.Replace("\\n", "\n");
			textBlock.TextWrapping = TextWrapping.Wrap;
			minWidth = Math.Max(minWidth, textBlock.ActualWidth);

			stackPanel1.Children.Add(textBlock);
			grid.Children.Add(stackPanel1);

			var stackPanel2 = new StackPanel();
			stackPanel2.SetValue(Grid.RowProperty, 2);
			stackPanel2.Orientation = Orientation.Horizontal;
			stackPanel2.HorizontalAlignment = HorizontalAlignment.Right;

			double buttonsWidth = 0;
			bool isFirstButton = true;
			foreach (var mBoxButton in buttons)
			{
				if (isFirstButton)
					_closingTag = mBoxButton.Tag;
				isFirstButton = false;

				var button = new Button { Content = mBoxButton.Caption };
				button.Tag = mBoxButton.Tag;
				buttonsWidth += button.ActualWidth;
				minWidth = Math.Max(minWidth, buttonsWidth);
				button.Click += button_Click;

				stackPanel2.Children.Add(button);
			}

			grid.Children.Add(stackPanel2);

			if (minWidth > 700)
				Width = 390;
			else
				Width = minWidth + 40;

			Height = textBlock.ActualHeight + 140; 

			Content = grid;
			if (style != null)
				Style = style;

			Closed += MBox_Closed;
		}

		void MBox_Closed(object sender, EventArgs e)
		{
			if (_callback != null)
				_callback(_closingTag);
		}

		private void button_Click(object sender, RoutedEventArgs e)
		{
			var button = sender as Button;
			if (button != null)
				_closingTag = button.Tag;

			Close();
		}
	}

	///<summary>
	///</summary>
	public class MessageWindowButton
	{
		///<summary>
		///</summary>
		public string Caption { get; set; }

		///<summary>
		///</summary>
		public object Tag { get; set; }
	}
}
